package org.dromara.datacheck.controller.jd;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.datacheck.domain.bo.jd.JdDirectTransferOrderBo;
import org.dromara.datacheck.domain.jd.JdDirectTransferOrder;
import org.dromara.datacheck.domain.vo.jd.JdDirectTransferOrderImportVo;
import org.dromara.datacheck.domain.vo.jd.JdDirectTransferOrderVo;
import org.dromara.datacheck.listener.jd.JdDirectTransferOrderImportListener;
import org.dromara.datacheck.service.jd.IJdDirectTransferOrderService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 金蝶-直接调拨单
 *
 * @author 刘武贵
 * @date 2024-09-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataCheck/jd/directTransferOrder")
public class JdDirectTransferOrderController extends BaseController {

    private final IJdDirectTransferOrderService jdDirectTransferOrderService;

    /**
     * 查询金蝶-直接调拨单列表
     */
    @SaCheckPermission("dataCheck:directTransferOrder:list")
    @GetMapping("/list")
    public TableDataInfo<JdDirectTransferOrderVo> list(JdDirectTransferOrderBo bo, PageQuery pageQuery) {
        return jdDirectTransferOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出金蝶-直接调拨单列表
     */
    @SaCheckPermission("dataCheck:directTransferOrder:export")
    @Log(title = "金蝶-直接调拨单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(JdDirectTransferOrderBo bo, HttpServletResponse response) {
        List<JdDirectTransferOrderVo> list = jdDirectTransferOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "金蝶-直接调拨单", JdDirectTransferOrderVo.class, response);
    }

    /**
     * 获取金蝶-直接调拨单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("dataCheck:directTransferOrder:query")
    @GetMapping("/{id}")
    public R<JdDirectTransferOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(jdDirectTransferOrderService.queryById(id));
    }

    /**
     * 新增金蝶-直接调拨单
     */
    @SaCheckPermission("dataCheck:directTransferOrder:add")
    @Log(title = "金蝶-直接调拨单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody JdDirectTransferOrderBo bo) {
        return toAjax(jdDirectTransferOrderService.insertByBo(bo));
    }

    /**
     * 修改金蝶-直接调拨单
     */
    @SaCheckPermission("dataCheck:directTransferOrder:edit")
    @Log(title = "金蝶-直接调拨单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody JdDirectTransferOrderBo bo) {
        return toAjax(jdDirectTransferOrderService.updateByBo(bo));
    }

    /**
     * 删除金蝶-直接调拨单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("dataCheck:directTransferOrder:remove")
    @Log(title = "金蝶-直接调拨单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(jdDirectTransferOrderService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "金蝶-直接调拨单导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        // 每次导入之前清空表里的数据
        List<JdDirectTransferOrder> jdOtherEnterWarhouseOrderList = jdDirectTransferOrderService.queryAll();
        if(CollUtil.isNotEmpty(jdOtherEnterWarhouseOrderList)){
            jdDirectTransferOrderService.deleteAll();
        }
        // 解析Excel文件并处理导入逻辑
        ExcelResult<JdDirectTransferOrderImportVo> result = ExcelUtil.importExcel(file.getInputStream(), JdDirectTransferOrderImportVo.class, new JdDirectTransferOrderImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 查询金蝶-直接调拨单列表
     * 场景分类：寄售-寄售调拨（猫超，进口猫超，京东自营）
     * 业务查询条件：根据jd_other_out_warehouse_order_customer客户为TYMO京东自营店、TYMO天猫超市、TYMO天猫进口超市、国内货代店、零星分销店铺铺查询
     */
    @SaCheckPermission("dataCheck:directTransferOrder:list")
    @GetMapping("/list-by-customer1")
    public TableDataInfo<JdDirectTransferOrderVo> listByCustomer1(JdDirectTransferOrderBo bo, PageQuery pageQuery) {
        bo.setCustomerList(Arrays.asList("TYMO京东自营店","TYMO天猫超市","TYMO天猫进口超市","国内货代店铺","零星分销店铺"));
        return jdDirectTransferOrderService.queryPageListByCustomer1(bo, pageQuery);
    }

    /**
     * 查询金蝶-直接调拨单列表
     * 场景分类：调拨-仓库间调拨
     * 业务查询条件：根据callout_warehouse调出仓库为销退仓，fold_warehouse调入仓库东莞仓查询
     */
    @SaCheckPermission("dataCheck:directTransferOrder:list")
    @GetMapping("/list-by-allot-warehouse1")
    public TableDataInfo<JdDirectTransferOrderVo> listByAllotWarehouse1(JdDirectTransferOrderBo bo, PageQuery pageQuery) {
        return jdDirectTransferOrderService.queryPageListByAllotWarehouse1(bo, pageQuery);
    }


    /**
     * 查询金蝶-直接调拨单列表
     * 场景分类：调拨-销退仓,次品仓调拨委外加工仓
     * 业务查询条件：根据receipt_number单据编号为QT结尾的单子查询
     */
    @SaCheckPermission("dataCheck:directTransferOrder:list")
    @GetMapping("/list-by-receipt-number1")
    public TableDataInfo<JdDirectTransferOrderVo> listByReceiptNumber1(JdDirectTransferOrderBo bo, PageQuery pageQuery) {
        return jdDirectTransferOrderService.queryPageListByReceiptNumber1(bo, pageQuery);
    }

    /**
     * 查询金蝶-直接调拨单列表
     * 场景分类：海外出口-海外出口（东莞仓)
     * 业务查询条件：根据jd_other_out_warehouse_order_customer客户为海外备货店铺查询
     */
    @SaCheckPermission("dataCheck:directTransferOrder:list")
    @GetMapping("/list-by-customer2")
    public TableDataInfo<JdDirectTransferOrderVo> listByCustomer2(JdDirectTransferOrderBo bo, PageQuery pageQuery) {
        bo.setCustomerList(Arrays.asList("海外备货店铺","国内货代店铺"));
        return jdDirectTransferOrderService.queryPageListByCustomer2(bo, pageQuery);
    }

    /**
     * 查询金蝶-直接调拨单列表
     * 场景分类：2C销售-销售发货
     * 业务查询条件：根据receipt_number单据编号为FH结尾且callout_warehouse调出仓库为东莞仓查询
     */
    @SaCheckPermission("dataCheck:directTransferOrder:list")
    @GetMapping("/list-by-receipt-number2")
    public TableDataInfo<JdDirectTransferOrderVo> listByReceiptNumber2(JdDirectTransferOrderBo bo, PageQuery pageQuery) {
        return jdDirectTransferOrderService.queryPageListByReceiptNumber2(bo, pageQuery);
    }

    /**
     * 查询金蝶-直接调拨单列表
     * 场景分类：2C销售-销售退货（售后单）
     * 业务查询条件：根据receipt_number单据编号为TH结尾且fold_warehouse调入仓库为销退仓查询
     */
    @SaCheckPermission("dataCheck:directTransferOrder:list")
    @GetMapping("/list-by-receipt-number3")
    public TableDataInfo<JdDirectTransferOrderVo> listByReceiptNumber3(JdDirectTransferOrderBo bo, PageQuery pageQuery) {
        return jdDirectTransferOrderService.queryPageListByReceiptNumber3(bo, pageQuery);
    }


}
